package com.crm.crm.controller;

import com.crm.crm.domain.ControllerLog;
import com.crm.crm.domain.dto.PageQueryParams;
import com.crm.crm.domain.vo.ControllerLogVo;
import com.crm.crm.domain.vo.PageResult;
import com.crm.crm.domain.vo.business.result.Result;
import com.crm.crm.service.LogService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@RestController
@Slf4j
@RequestMapping("/monitor")
public class LogController {
    @Autowired
    private LogService logService;
    @GetMapping("/operlog/list")
    public Result selectLogs(PageQueryParams pageQueryParams, @RequestParam Map<String,String> params){
        pageQueryParams.setBeginCreateTime(params.get("params[beginTime]"));
        pageQueryParams.setEndCreateTime(params.get("params[endTime]"));
        log.info("分页查询参数：{}", pageQueryParams);
        PageResult<ControllerLogVo> logs = logService.findLogs(pageQueryParams);

        return Result.success().put("total", logs.getTotal()).put("rows", logs.getRows());
    }
    /*批量删除日志*/
    @DeleteMapping("/operlog/{ids}")
    public Result deleteLogs(@PathVariable List<Long> ids){
        logService.removeByIds(ids);
        return Result.success();
    }
    /*清空日志*/
    @DeleteMapping("/operlog/clean")
    public Result cleanLogs(){
        logService.remove(null);
        logService.truncateTable();
        return Result.success();
    }
}